package com.mvvm.library.util;


import android.app.Activity;
import android.app.Application;
import android.app.ProgressDialog;
import android.content.Context;
import android.view.inputmethod.InputMethodManager;


/**
 * @创建时间：2022/7/13
 * @作者：小蔡
 * @功能介绍：Application工具
 */
public class XUtil {
    private static Application mApplicationContext;
    private static ProgressDialog progressDialog;

    public static void initialize(Application app) {
        mApplicationContext = app;
    }

    public static Application getApplication() {
        return mApplicationContext;
    }


    /**
     * 在当前 activity 显示加载中
     *
     * @param msg 信息
     */
    public static void showLoading(String msg) {
        progressDialog = ProgressDialog.show(ActivityUtil.getCurrentActivity(), "", msg, true, true);
        progressDialog.setCanceledOnTouchOutside(true);
        progressDialog.show();
    }

    /**
     * 在指定 activity 显示加载中
     *
     * @param activity activity
     * @param msg 信息
     */
    public static LoadingDialog dialog;

    public static void showLoading(Activity activity, String msg) {
        dialog = new LoadingDialog(activity, msg);//显示Dialog
        dialog.show();//关闭Dialog


//        progressDialog = ProgressDialog.show(activity, "", msg, true, true);
//        progressDialog.setCanceledOnTouchOutside(true);
//        progressDialog.show();
    }

    public static void dismissLoading() {
        if (dialog != null && dialog.mLoadingDialog.isShowing()) {
            dialog.close();
        }

//        if (progressDialog != null && progressDialog.isShowing()) {
//            progressDialog.dismiss();
//        }
    }

    public static void closeSoftKeyboard() {
        InputMethodManager inputManger = (InputMethodManager) ActivityUtil.getCurrentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputManger != null) {
            inputManger.hideSoftInputFromWindow(ActivityUtil.getCurrentActivity().getWindow().getDecorView().getWindowToken(), 0);
        }
    }

}
